package com.smsc.headend.task.engine.registry;

import com.smsc.headend.module.push.entity.ConfigNotifyType;
import com.smsc.headend.task.engine.service.push.PushDataDecodeHandler;
import com.smsc.headend.task.engine.service.push.impl.DcuRegistryHandlerImpl;
import com.smsc.headend.task.engine.service.push.impl.EventAndAlarmHandlerImpl;
import com.smsc.headend.task.engine.service.push.impl.GenericProfileDataHandlerImpl;
import com.smsc.headend.task.engine.service.push.impl.UpgradeResultHandlerImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * (业务实现)
 *
 * @author wjh
 * @date 2022/6/17 9:07.
 */
@Component
@Slf4j
public class PushDataDecodeRegistry {

    @Autowired
    AutowireCapableBeanFactory autowireCapableBeanFactory;

    Map<Integer, PushDataDecodeHandler> pushDataDecodeHandlerMap = new HashMap<>();

    DcuRegistryHandlerImpl dcuRegistryHandler;
    EventAndAlarmHandlerImpl eventAndAlarmHandler;
    GenericProfileDataHandlerImpl genericProfileDataHandler;
    UpgradeResultHandlerImpl upgradeResultHandler;

    @PostConstruct
    public void load() {
        dcuRegistryHandler = new DcuRegistryHandlerImpl();
        eventAndAlarmHandler = new EventAndAlarmHandlerImpl();
        genericProfileDataHandler = new GenericProfileDataHandlerImpl();
        upgradeResultHandler = new UpgradeResultHandlerImpl();
        autowireCapableBeanFactory.autowireBean(dcuRegistryHandler);
        autowireCapableBeanFactory.autowireBean(eventAndAlarmHandler);
        autowireCapableBeanFactory.autowireBean(genericProfileDataHandler);
        autowireCapableBeanFactory.autowireBean(upgradeResultHandler);
        pushDataDecodeHandlerMap.put(ConfigNotifyType.MessageTypeEnum.DCU_REGISTRY.getCode(), dcuRegistryHandler);
        pushDataDecodeHandlerMap.put(ConfigNotifyType.MessageTypeEnum.ALARM.getCode(), eventAndAlarmHandler);
        pushDataDecodeHandlerMap.put(ConfigNotifyType.MessageTypeEnum.EVENT.getCode(), eventAndAlarmHandler);
        pushDataDecodeHandlerMap.put(ConfigNotifyType.MessageTypeEnum.GENERIC_PROFILE_DATA.getCode(), genericProfileDataHandler);
        pushDataDecodeHandlerMap.put(ConfigNotifyType.MessageTypeEnum.FIRMWARE_UPGRADE_RESULT.getCode(), upgradeResultHandler);
    }

    public PushDataDecodeHandler getService(Integer messageTypeCode) {
        PushDataDecodeHandler handler = pushDataDecodeHandlerMap.get(messageTypeCode);
        if (handler == null) {
            log.error("{} have no push data handler", messageTypeCode);
        }
        return handler;
    }
}
